package com.gxa.pingan.controller;

import com.alibaba.excel.EasyExcel;
import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.DriverMapper;
import com.gxa.pingan.pojo.Driver;
import com.gxa.pingan.pojo.OrderVo;
import com.gxa.pingan.service.DriverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @Author ChenJin
 * @date 2021/10/25 15:33
 * Description:
 **/
@CrossOrigin
@RestController
@RequestMapping(value = "driver")
@Api(value = "执行司机操作的控制器", tags = "司机接口的api")
public class DriverController {

    /**
     * 导出司机信息的接口方法
     * 用户可通过列表第一列的选择按钮选择需要导出的内容
     *
     * @return Map--需要给成功的提示吗?
     * @throws IOException 异常
     */
    @PostMapping("/export")
    @ApiOperation(value = "导出司机信息的接口方法")
    public ResponseEntity<byte[]> exportOrderList() throws IOException, BusinessException {
        // 查询到需要导出的集合
        List<Driver> lists = driverService.upload();
        System.out.println(lists);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // 语雀的核心写操作
        EasyExcel.write(outputStream, Driver.class).sheet("司机信息表").doWrite(lists);
        // 缓冲数组
        byte[] bytes = outputStream.toByteArray();
        // 获取请求头信息
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求头,告诉浏览器，以附件方式打开，而不是在页面上浏览
        httpHeaders.setContentDispositionFormData("attachment", URLEncoder.encode("司机信息表.xlsx", "utf-8"));
        //httpHeaders.setContentDispositionFormData("attachment", "司机信息表.xlsx");
        return new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);
    }

    @Resource
    private DriverService driverService = null;

    @PostMapping("/insert")
    @ApiOperation("添加司机")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "driver", paramType = "body")
    })
    public Map<String, Object> insert(@RequestBody Map<String,Object> driver) throws BusinessException {
        driverService.insert(driver);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("修改司机信息")
    @ApiImplicitParam(name = "driver", value = "driver", paramType = "body", required = true)
    public Map<String, Object> update(@RequestBody Map<String,Object> driver) throws BusinessException {
        driverService.update(driver);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    /**
     * @param value 姓名或者手机号
     * @return
     */
    @PostMapping("/get")
    @ApiOperation("根据姓名或者手机号查找司机")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "value", value = "value", paramType = "query", required = false),
            @ApiImplicitParam(name = "page", value = "page", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query", required = true)
    })
    public Map<String, Object> get(String value, Integer page, Integer size) throws BusinessException {
        List<Driver> list = driverService.getList(value, page, size);
        Integer driverSize = driverService.getSize(value);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("获取成功");
        jsonResult.setData(list);
        jsonResult.put("count",driverSize);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getDriver")
    @ApiOperation("修改司机信息时，获取司机信息")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true)
    public Map<String, Object> getDriver(Long id) throws BusinessException {
        Driver driver = driverService.getDriver(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("获取成功");
        jsonResult.setData(driver);
        return jsonResult.getDataMap();
    }
}
